package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class MySurfaceViewRobot extends BaseSurfaceView {
    private Bitmap mBitmap;
    private float mX;
    private float mY;
    private float mScreenWidth;
    private float mScreenHeight;
    private boolean isRight = true;
    private boolean isDown = true;
    public MySurfaceViewRobot(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MySurfaceViewRobot(Context context) {
        super(context);
        init();
    }
    private void init(){
        mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
//        DisplayMetrics metrics = new DisplayMetrics();
//        mScreenWidth=metrics.widthPixels;
//        mScreenHeight = metrics.heightPixels;
        //得到WindowManager
        WindowManager wMgr = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wMgr.getDefaultDisplay().getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
    }

    @Override
    protected void onPaint(Canvas canvas, Paint paint) {
        canvas.drawBitmap(mBitmap,mX,mY,paint);
    }

    @Override
    protected void onUpdate() {
        if(isRight){
            mX+=5;
            isRight = mX + mBitmap.getWidth() >= mScreenWidth?false:true;
        }
        else{
            mX-=5;
            isRight = mX <=0?true:false;
        }
        if(isDown){
            mY+=5;
            isDown = mY + mBitmap.getWidth() >= mScreenHeight?false:true;
        }
        else{
            mY-=5;
            isDown = mY <= 0 ?true:false;
        }
    }
}
